package com.yt.dor.api.cms;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.yt.common.constant.Result;
import com.yt.common.enums.CodeEnum;
import com.yt.common.utils.StrUtils;
import com.yt.dor.models.vo.DormRoomVO;
import com.yt.dor.models.vo.ViolationRecordVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;
import java.util.Arrays;
import java.util.List;
import com.yt.dor.models.service.IViolationRecordService;
import com.yt.dor.models.bo.ViolationRecordSaveBO;
import com.yt.dor.models.bo.ViolationRecordBO;
import com.yt.dor.models.dto.ViolationRecordDTO;

/**
 * 违规表详情控制器
 *
 * @author thekai
 * @Date 2025-10-28 16:38:28
 */

@RestController
@RequestMapping("/cms/violationRecord")
@Slf4j
@Api(value = "ViolationRecordController",tags = "违规表详情接口")
public class ViolationRecordController {

    @DubboReference
    private IViolationRecordService violationRecordService;

    @ApiOperation(value = "添加违规表详情",notes = "添加违规表详情")
    @PostMapping(value = "/insert")
    public Result insert(ViolationRecordSaveBO saveBO) {
        boolean flag = violationRecordService.add(saveBO);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100007);
        }
    }

    @PostMapping("/save")
    @ApiOperation(value = "保存违规表详情",notes = "保存违规表详情")
    public Result save(ViolationRecordSaveBO saveBO){
        if(saveBO.getId() == null){
            //添加
            return  insert(saveBO);
        } else {
            return  update(saveBO);
        }
    }

    @ApiOperation(value = "修改违规表详情",notes = "修改违规表详情")
    @PostMapping("/update")
    public Result update(ViolationRecordSaveBO saveBO){
        ViolationRecordDTO dto = new ViolationRecordDTO();
        BeanUtils.copyProperties(saveBO, dto);
        dto.setId(saveBO.getId());
        boolean flag = violationRecordService.updateById(dto);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100008);
        }
    }


    @ApiOperation(value = "删除违规表详情",notes = "删除违规表详情")
    @ApiImplicitParam(name = "id",value = "违规表详情id",required = true,paramType = "query",dataType = "Long")
    @PostMapping("/delete")
    public Result delete(@RequestParam("id") Long id){
        boolean flag = violationRecordService.deleteById(id);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100009);
        }
    }

    @PostMapping("/deleteBatch")
    @ApiOperation(value = "批量删除违规表详情",notes = "批量删除违规表详情")
    @ApiImplicitParam(name = "ids",value = "ids:以逗号隔开",required = true,paramType = "query", dataType = "String")
    public Result deleteBatch(@RequestParam("ids") String ids){
        if(StrUtils.isEmpty(ids)){
            return new Result(CodeEnum.FAIL_100002 );
        }
        String[] arr = ids.split(",");
        violationRecordService.deleteBatchIds(Arrays.asList(arr));
        return new Result<>();
    }

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找",notes = "根据id查找")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<ViolationRecordDTO> get(@RequestParam("id") Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }

        ViolationRecordDTO dto = violationRecordService.selectById(id);

        return new Result<>(dto);
    }

    @ApiOperation(value = "违规表详情列表",notes = "违规表详情列表")
    @PostMapping("/queryPageList")
    public Result<Page<ViolationRecordDTO>> queryPageList(ViolationRecordBO bo){
        Wrapper<ViolationRecordDTO> wrapper = new EntityWrapper<>();


        if (bo.getStartDate() != null){
            wrapper.ge("create_time", bo.getStartDate());
        }
        if (bo.getEndDate() != null){
            wrapper.le("create_time", bo.getEndDate());

        }
        Page<ViolationRecordDTO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        page = violationRecordService.selectPage(page, wrapper);

        return new Result<>(page);
    }
    @ApiOperation(value = "添加违规表详情",notes = "添加违规表详情")
    @PostMapping(value = "/AddViolationRecord")
    public Result AddViolationRecord(@RequestBody ViolationRecordSaveBO saveBO) {
        boolean flag = violationRecordService.add(saveBO);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100007);
        }
    }


    @ApiOperation(value = "删除违规表详情",notes = "删除违规表详情")
    @ApiImplicitParam(name = "id",value = "违规表详情id",required = true,paramType = "query",dataType = "Long")
    @PostMapping("/deleteAddViolationRecord")
    public Result deleteAddViolationRecord(@RequestParam("id") Long id){
        boolean flag = violationRecordService.deleteById(id);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100009);
        }
    }

    @ApiOperation(value = "修改违规表详情",notes = "修改违规表详情")
    @PostMapping("/updateViolationRecord")
    public Result updateViolationRecord(@RequestBody ViolationRecordSaveBO saveBO){
        ViolationRecordDTO dto = new ViolationRecordDTO();
        BeanUtils.copyProperties(saveBO, dto);
        dto.setId(saveBO.getId());
        boolean flag = violationRecordService.updateById(dto);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100008);
        }
    }
    @PostMapping("/queryViolationRecord")
    @ApiOperation(value = "查询违规表详情", notes = "查询违规表详情")
    public Result<List<ViolationRecordVO>> queryViolationRecord() {
        List<ViolationRecordVO> violationRecordVOS = violationRecordService.query();
        return new Result<>(violationRecordVOS);
    }

    // 在ViolationRecordController.java中添加
    @PostMapping("/statistics")
    @ApiOperation(value = "统计违规情况", notes = "按学期和房间统计违规次数及总扣分")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "semester", value = "学期（如20241）", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "roomId", value = "房间ID", paramType = "query", dataType = "Long")
    })
    public Result<List<ViolationRecordVO>> statistics(
            @RequestParam(required = false) Integer semester,
            @RequestParam(required = false) Long roomId) {
        try {
            List<ViolationRecordVO> statistics = violationRecordService.statistics(semester, roomId);
            return new Result<>(statistics);
        } catch (Exception e) {
            log.error("统计违规情况失败", e);
            return new Result<>(CodeEnum.FAIL_100008); // 假设新增了统计失败的枚举值
        }
    }
}
